<?php
function cdm($code, $data = '', $msg = ''){
	return json(compact('code', 'data', 'msg'));
}

function tokenEncrypt($data){
	$key = md5('huantu');
	$iv = substr(md5($key),0,16);
	$base64 = base64_encode(openssl_encrypt($data,'AES-128-CBC',$key,OPENSSL_RAW_DATA,$iv));
	$base64 = str_replace('/','-',$base64);
	$base64 = str_replace('+','_',$base64);
	return str_replace('=','*',$base64);
}

function tokenDecrypt($data){
	$data = str_replace('*','=',$data);
	$data = str_replace('-','+',$data);
	$data = str_replace('_','=',$data);
	$key = md5('huantu');
	$iv = substr(md5($key),0,16);
	return openssl_decrypt(base64_decode($data),'AES-128-CBC',$key,OPENSSL_RAW_DATA,$iv);
}

function request_input($name='',$default=''){
	if(request()->isGet()){
		$arr = request()->get();
		if(!$arr){
			$arr = request()->param();
		}
	}else{
		$input = file_get_contents("php://input");
		if($input){
			$arr = json_decode($input,true);
		}else{
			$arr = request()->param();
		}
		if(is_null($arr)){
			$xml = simplexml_load_string($input,"SimpleXMLElement", LIBXML_NOCDATA);
			if($xml){
				$arr = json_decode(json_encode($xml),true);
			}else{
				parse_str($input,$arr);
			}
		}
	}
	if(empty($name)){
		return $arr;
	}else{
		if(isset($arr[$name])){
			if(!is_array($arr[$name])){
				if(substr($arr[$name],0,1) == '[' && substr($arr[$name],-1,1) == ']'){
					$arr[$name] = json_decode($arr[$name],true);
				}
			}
		}
		return isset($arr[$name]) ? $arr[$name] : $default;
	}
}

function getTree($arr, $index){
	$tree = [];
	foreach ($arr as $k => $v) {
		if ($v[$index] != 0) {
			$arr[$v[$index]]['children'][] = &$arr[$k];
		}else{
			$tree[] = &$arr[$k];
		}
	}
	return $tree;
}

//一维数据数组生成数据树
function arr2tree($list, $id = 'id', $fid = 'fid', $children = 'children'){
	list($tree, $map) = [[], []];
	foreach ($list as $item) {
		$map[$item[$id]] = $item;
	}
	foreach ($list as $item) {
		if (isset($item[$fid]) && isset($map[$item[$fid]])) {
			$map[$item[$fid]][$children][] = &$map[$item[$id]];
		} else {
			$tree[] = &$map[$item[$id]];
		}
	}
	unset($map);
	return $tree;
}

//二维数组去掉重复值
function assoc_unique($arr, $key = 'id') {
	$tmp_arr = array();
	foreach ($arr as $k => $v) {
		if (in_array($v[$key], $tmp_arr)) {
			unset($arr[$k]);
		} else {
			$tmp_arr[] = $v[$key];
		}
	}
	sort($arr);
	return $arr;
}

function getKeyById($arr, $id){
	$key = -1;
	foreach ($arr as $k => $v) {
		if($v['id'] == $id){
			$key = $k;
		}
	}
	return $key;
}

function oldFid2newFid($old, $new){
	foreach ($new as $nkey => &$nvalue) {
		if($nvalue['fid'] > 0){
			$key = getKeyById($old,$nvalue['fid']);
			if($key > -1){
				$nvalue['fid'] = $new[$key]['id'];
			}
		}
		unset($nvalue['create_time']);
		unset($nvalue['update_time']);
	}
	return $new;
}

//字符串分割
function explodeStr($str) {
	$len = mb_strlen($str, 'utf-8');
	for ($i = 0; $i < $len; $i++) {
		$arr[] = mb_substr($str, $i, 1, 'utf-8');
	}
	return $arr;
}

function unzip($filePath, $path) {
	if (empty($path) || empty($filePath)) {
		return false;
	}
	$zip = new ZipArchive();
	if ($zip->open($filePath) === true) {
		$zip->extractTo($path);
		$zip->close();
		return true;
	} else {
		return false;
	}
}

function isPhone($num) {
	if (preg_match("/^1((34[0-8]\d{7})|((3[0-3|5-9])|(4[5-7|9])|(5[0-3|5-9])|(66)|(7[2-3|5-8])|(8[0-9])|(9[1|8|9]))\d{8})$/", $num)) {
		return true;
	} else {
		return false;
	}
}

function str_substr($start, $end, $str){
	$temp = explode($start, $str, 2);
	if(count($temp) > 1){
		$content = explode($end, $temp[1], 2);
		return $content[0];
	}
}
